<?php

namespace app\admin\model;

use think\Model;


class AssistantOrder extends Model
{

    

    

    // 表名
    protected $name = 'assistant_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'start_time_text',
        'end_time_text',
        'create_time_text',
        'status_text'
    ];
    

    
    public function getStatusList()
    {
        return ['paying' => __('待支付'), 'payed' => __('已支付'), 'taked' => __('已接单'), 'doing' => __('进行中'), 'done' => __('已完成'), 'refund' => __('已退款')];
    }


    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function getStatusTxt($status)
    {

        if($status == 'payed'){
            return "待接单";
        }else if($status == 'taked'){
            return "已接单";
        }else if($status == 'doing'){
            return "消费中";
        }else if($status == 'done'){
            return "已完成";
        }else if($status == 'cancel'){
            return "已取消";
        }else if($status == 'refund'){
            return "已退款";
        }

    }


    public function shop()
    {
        return $this->belongsTo('Shop', 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function assist()
    {
        return $this->belongsTo('Assistant', 'assistant_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

}
